package com.iye.puretennis.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import cn.jpush.android.api.JPushInterface;
import com.iye.puretennis.R;
import com.iye.puretennis.custom.CustomApplication;
import com.iye.puretennis.custom.ExitApplication;
import com.iye.puretennis.helper.Constant;
import com.iye.puretennis.helper.DBManager;
import com.iye.puretennis.helper.RemoteUrlCollections;
import com.iye.puretennis.helper.ServerAccessHelper;
import com.iye.puretennis.utils.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Author: Chuanbao on 2014/11/20.
 */
public class CommonLoginActivity extends Activity {
    DBManager dbManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comm_login);
        dbManager = new DBManager(this);
        CustomApplication application = (CustomApplication) getApplication();
        if (application.getHuaWenHuPo() == null) {
            application.setHuaWenHuPo(Typeface.createFromAsset(getAssets(), Constant.DEFAULT_FONT));
        }
        ExitApplication.getInstance().addActivity(this);

    }

    public void login(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                EditText usernameView = (EditText) findViewById(R.id.username);
                EditText passwordView = (EditText) findViewById(R.id.password);
                EditText serverHostView = (EditText) findViewById(R.id.server_host);
                String username = usernameView.getText().toString();
                String password = passwordView.getText().toString();
                RemoteUrlCollections.REMOTE_SERVER_HOST = serverHostView.getText().toString();
                process(username, password);
            }
        }).start();

    }

    public void register(View view) {

        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

    }

    private void process(String username, String password) {

        try {

            String rev = ServerAccessHelper.login(username, password);
            if (StringUtils.hasText(rev)) {
                JSONObject result = new JSONObject(rev);
                String guid = result.getString("guid");
                String token = result.getString("token");
                boolean setup = result.getBoolean("setup");
                dbManager.login(guid, token, setup ? 1 : 0);
                //start the profile activity
                Intent intent;
                if (setup) {
                    intent = new Intent(this, MainTabsActivity.class);
                } else {
                    intent = new Intent(this, ProfileActivity.class);
                }
                intent.putExtra("guid", guid);
                intent.putExtra("Access_Token", token);
                startActivity(intent);
                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {

        super.onResume();

        JPushInterface.onResume(this);

    }

    @Override
    protected void onPause() {

        super.onPause();

        JPushInterface.onPause(this);

    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


}